package cn.cxyxj.study.webflux02.account;

import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements ReactiveUserDetailsService {

    private static Map<String, UserDetails> users = new HashMap(2);
    static {
        users.put("dev", User.withUsername("dev").password("123456").roles("dev").build());
        users.put("test", User.withUsername("test").password("123").roles("test").build());
    }

    @Override
    public Mono<UserDetails> findByUsername(String username) {
        UserDetails userDetails = users.get(username);
        // 构造出一个Mono
        return (userDetails != null) ? Mono.just(userDetails) : Mono.empty();
    }

}
